package com.stephentuso.welcome;

import com.stephentuso.welcome.util.PageView;
import com.stephentuso.welcome.util.ResUtil;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.utils.PacMap;
import org.jetbrains.annotations.Nullable;

/**
 * A simple PageView that shows an image and a title.
 * Use the {@link WelcomeTitlePageView#newInstance} factory method to
 * create an instance of this PageView.
 */
public class WelcomeTitlePageView extends PageView implements WelcomePage.OnChangeListener {

    // the PageView initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_DRAWABLE_ID = "drawable_id";
    private static final String ARG_TITLE = "title";
    private static final String ARG_SHOW_ANIM = "show_anim";
    private static final String ARG_TYPEFACE_PATH = "typeface_path";
    private static final String ARG_TITLE_COLOR = "title_color";

    private int drawableId;
    private String title = "";
    private boolean showParallaxAnim = true;
    private Text titleView = null;
    private Image imageView = null;

    /**
     * Use this factory method to create a new instance of
     * this PageView using the provided parameters.
     *
     * @param context calling context
     * @param resId image resource id
     * @param title page title
     * @param showParallaxAnim true for parallax animation
     * @param typefacePath title text typeface
     * @param titleColor title text color
     * @return A new instance of WelcomeTitlePageView.
     */
    public static WelcomeTitlePageView newInstance(Context context,
                                                   int resId,
                                                   String title,
                                                   boolean showParallaxAnim,
                                                   @Nullable String typefacePath,
                                                   int titleColor)
    {
        PacMap args = new PacMap();
        args.putIntValue(ARG_DRAWABLE_ID, resId);
        args.putString(ARG_TITLE, title);
        args.putBooleanValue(ARG_SHOW_ANIM, showParallaxAnim);
        args.putString(ARG_TYPEFACE_PATH, typefacePath);
        args.putIntValue(ARG_TITLE_COLOR, titleColor);
        WelcomeTitlePageView pageView = new WelcomeTitlePageView(context, args);
        return pageView;
    }

    public WelcomeTitlePageView(Context context, PacMap args) {
        super(context);
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_wel_page_view_title, this, true);
        imageView = (Image) component.findComponentById(ResourceTable.Id_wel_image);
        titleView = (Text) component.findComponentById(ResourceTable.Id_wel_title);

        drawableId = args.getIntValue(ARG_DRAWABLE_ID);
        title = args.getString(ARG_TITLE);

        imageView.setImageElement(ResUtil.getVectorDrawable(context, drawableId));
        titleView.setText(title);

        int titleColor = args.getIntValue(ARG_TITLE_COLOR, WelcomeUtils.NO_COLOR_SET);
        if (titleColor != WelcomeUtils.NO_COLOR_SET)
            titleView.setTextColor(new Color(titleColor));

        showParallaxAnim = args.getBooleanValue(ARG_SHOW_ANIM, showParallaxAnim);

        WelcomeUtils.setTypeface(titleView, args.getString(ARG_TYPEFACE_PATH), getContext());

    }

    @Override
    public void onWelcomeScreenPageScrolled(int pageIndex, float offset, int offsetPixels) {
        /*if (showParallaxAnim && imageView != null) {
            imageView.setTranslationX(-offsetPixels * 0.8f);
        }*/
    }

    @Override
    public void onWelcomeScreenPageSelected(int pageIndex, int selectedPageIndex) {
        //Not used
    }

    @Override
    public void onWelcomeScreenPageScrollStateChanged(int pageIndex, int state) {
        //Not used
    }
}
